package com.yyfax.ph.personal.authority.forgot;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.Loader;
import android.util.TypedValue;
import butterknife.BindView;
import com.yyfax.common.noscrolll.NoScrollerViewPager;
import com.yyfax.common.tools.DensityUtil;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.ph.BuildConfig;
import com.yyfax.ph.R;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.base.adapter.FragmentAdapter;
import com.yyfax.ph.main.MainActivity;
import com.yyfax.ph.personal.authority.verifycode.VerifyCodeModel;
import com.yyfax.ph.personal.authority.verifycode.VerifyCodePresenter;

import java.util.ArrayList;
import java.util.List;

public class ForgotPwdActivity extends BaseYYFaxActivity implements ForgotPwdPresenter.View, VerifyCodePresenter.View {
	private static final int LOAD_ID_VERIFY_CODE = 1;
	private static final int LOAD_ID_FORGET_PWD = 2;

	@BindView(R.id.auto_view_pager)
	NoScrollerViewPager mPager;

	private VerifyCodeFragment mFragmentVerifyCode;
	private NewPwdFragment mFragmentNewPwd;

	private ForgotPwdPresenter mForgotPwdPresenter;
	private VerifyCodePresenter mVerifyCodePresenter;

	// 验证码校验成功后返回的校验订单号
	private String mVerifyOrderCode;

	public static void start(Activity aty) {
		Intent intent = new Intent(aty, ForgotPwdActivity.class);
		aty.startActivity(intent);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		switch (id) {
			case LOAD_ID_VERIFY_CODE:
				return new PresenterLoader<>(this, new VerifyCodePresenter.Generator());
			case LOAD_ID_FORGET_PWD:
				return new PresenterLoader<>(this, new ForgotPwdPresenter.Generator());
			default:
				break;
		}
		return null;
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		switch (loader.getId()) {
			case LOAD_ID_VERIFY_CODE:
				mVerifyCodePresenter = (VerifyCodePresenter) data;
				mVerifyCodePresenter.attach(this);
				break;
			case LOAD_ID_FORGET_PWD:
				mForgotPwdPresenter = (ForgotPwdPresenter) data;
				mForgotPwdPresenter.attach(this);
				break;
			default:
				break;
		}
	}

	@Override
	public int getContentViewRes() {
		return R.layout.find_code_activity_layout;
	}

	@Override
	public void initContentView() {
		setBtnBackVisible(true);
		setBtnRightVisible(false);
		setTitle(R.string.find_pwd);
		setTitleVisible(true);
		mTitleCenter.setTextSize(TypedValue.COMPLEX_UNIT_PX, DensityUtil.dip2px(this, 18));
		mTitleCenter.setTextColor(getResources().getColor(R.color.color_262932));
		mBtnBack.setImageDrawable(getResources().getDrawable(R.drawable.btn_login_back));
		mFragmentVerifyCode = new VerifyCodeFragment();
		mFragmentNewPwd = new NewPwdFragment();
		List<Fragment> fragments = new ArrayList<>();
		fragments.add(mFragmentVerifyCode);
		fragments.add(mFragmentNewPwd);

		FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
		mPager.setAdapter(adapter);
		mPager.setOffscreenPageLimit(fragments.size());
		mPager.setCurrentItem(0, true);
	}

	@Override
	protected void onStart() {
		super.onStart();
		getSupportLoaderManager().initLoader(LOAD_ID_VERIFY_CODE, null, this);
		getSupportLoaderManager().initLoader(LOAD_ID_FORGET_PWD, null, this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		getSupportLoaderManager().destroyLoader(LOAD_ID_VERIFY_CODE);
		getSupportLoaderManager().destroyLoader(LOAD_ID_FORGET_PWD);
	}

	@Override
	public void onBackBtnPressed() {
		super.onBackBtnPressed();
		int index = mPager.getCurrentItem();
		if (index > 0) {
			setTitle(R.string.find_pwd);
			mPager.setCurrentItem(--index, true);
		} else {
			finish();
		}
	}

	@Override
	public ForgotPwdModel.ForgotPwdParams getForgotPwdParams() {
		ForgotPwdModel.ForgotPwdParams forgotPwdParams = new ForgotPwdModel.ForgotPwdParams();
		forgotPwdParams.loginName = mFragmentVerifyCode.getCellPhone(); // 用户名（手机号）
		forgotPwdParams.password = mFragmentNewPwd.getNewCode(); // 新密码
		forgotPwdParams.verifyId = mVerifyOrderCode; // 校验订单号
		forgotPwdParams.retId = YJPH.getInstance().getPreferenceManager().getJPushRegId();
		return forgotPwdParams;
	}

	public void resetPassword() {
		mForgotPwdPresenter.retrievePwd();
	}

	public void getVeryCode() {
		mVerifyCodePresenter.obtainVerifyCode(VerifyCodeModel.VERIFY_CODE_TYPE_FORGET_PWD);
	}

	public void checkPhoneCode() {
		mVerifyCodePresenter.checkVerifyCode(VerifyCodeModel.VERIFY_CODE_TYPE_FORGET_PWD);
	}

	@Override
	public VerifyCodeModel.VerifyCodeParams getVerifyParams(String verifyCodeType) {
		VerifyCodeModel.VerifyCodeParams verifyCodeParams = new VerifyCodeModel.VerifyCodeParams();
		verifyCodeParams.phone = mFragmentVerifyCode.getCellPhone();
		verifyCodeParams.type = verifyCodeType;
		verifyCodeParams.code = mFragmentVerifyCode.getVerifyCode();
		return verifyCodeParams;
	}

	@Override
	public void obtainVerifyCodeSuccess(String verifyCode) {
		//debug模式下自动填充 生产模式下不会进行
		if (BuildConfig.DEBUG) {
			mFragmentVerifyCode.setVerifyCode(verifyCode);
		}

		mFragmentVerifyCode.startCount();
		dismissLoading();
		showToast(R.string.verification_code_sent);
	}

	@Override
	public void obtainVerifyCodeFailure(String tips) {
		dismissLoading();
		showToast(tips);
	}

	@Override
	public void checkVerifyCodeSuccess(String verifyOrderCode) {
		dismissLoading();
		mVerifyOrderCode = verifyOrderCode;
		int index = mPager.getCurrentItem();
		if (index == 0) {
			index++;
			setTitle(R.string.set_login_pwd);
			mPager.setCurrentItem(index, true);
		}
	}

	@Override
	public void checkVerifyCodeFailure(String tips) {
		dismissLoading();
		showToast(tips);
		if (getString(R.string.verify_code_error).equals(tips)) {
			mFragmentVerifyCode.mPhoneVerifyTv.getText().clear();
		}
	}

	@Override
	public void retrievePwdSuccess() {
		dismissLoading();
		showToast(R.string.find_passwrod_success_relogin);
		YJPH.getInstance().onLogoutSuccess();
		MainActivity.start(this, true);
	}

	@Override
	public void retrievePwdFailure(String tips) {
		dismissLoading();
		showToast(tips);
	}

	/**
	 * TopBar变成白色
	 */
	@Override
	public int getTopBarBackgroundColor() {
		return ContextCompat.getColor(this, R.color.color_fafdff);
	}
}
